package yunwo.cn.yzstation.view.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.andview.refreshview.XRefreshView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.adapter.TrainnumberAdapter;
import yunwo.cn.yzstation.api.TrainnumberView;
import yunwo.cn.yzstation.base.MyBaseActivity;
import yunwo.cn.yzstation.bean.CinfoBean;
import yunwo.cn.yzstation.presenter.TrainnumberPersenter;

/**
 * Created by lao on 2017/10/14.
 */
public class TrainnumberActivity extends MyBaseActivity implements TrainnumberView {

    private XRefreshView mXrefreshview;
    private ListView mTrainnumber_lv;
    private TextView mZhuangtaiText;
    private LinearLayout mZhuangtai;
    private ImageView mZhuangtaiIMG;
    private TrainnumberPersenter mTrainnumberPersenter;
    private ProgressDialog mMyDialog;
    CinfoBean.DataBean mDataBean;
    private List<CinfoBean.DataBean.ListBean> mList;
    private TrainnumberAdapter mTrainnumberAdapter;
    int cout = 1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mMyDialog == null) {
            mMyDialog = ProgressDialog.show(TrainnumberActivity.this, null, "请稍等...");
            mMyDialog.setCancelable(false);
            mMyDialog.show();
        }
        initView();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        mTrainnumberPersenter.getCarData("",1);

    }

    private void initView() {
        mXrefreshview = (XRefreshView) findViewById(R.id.xrefreshview);
        mTrainnumber_lv = (ListView) findViewById(R.id.trainnumber_lv);
        mZhuangtai = (LinearLayout) findViewById(R.id.zhuangtai);
        mZhuangtaiText = (TextView) findViewById(R.id.zhuangtaiText);
        mZhuangtaiIMG = (ImageView) findViewById(R.id.zhuangtaiIMG);
        mXrefreshview.setPullLoadEnable(true);
        mXrefreshview.setPullRefreshEnable(true);
        mXrefreshview.setAutoLoadMore(false);
        mXrefreshview.setScrollBackDuration(300);
        mTrainnumberPersenter = new TrainnumberPersenter(this,this);
        mTrainnumberAdapter = new TrainnumberAdapter(TrainnumberActivity.this);
        mTrainnumberAdapter.refreshDataAndNotifyDataSetChanged(mList);
        mTrainnumber_lv.setAdapter(mTrainnumberAdapter);
        mXrefreshview.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onRefresh(boolean isPullDown) {

                mTrainnumberPersenter.getCarData("1",1);

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                if (mDataBean!=null &&mDataBean.isHasNextPage()) {
                    cout += 1;
                    mTrainnumberPersenter.getCarData("2",cout);
                    mXrefreshview.stopLoadMore();
                }else {
                    Toast.makeText(TrainnumberActivity.this, "已加载全部数据", Toast.LENGTH_SHORT).show();
                    mXrefreshview.stopLoadMore(false);
                }

            }
        });
    }

    @Override
    protected String settiltile() {
        return "车次信息";
    }

    @Override
    protected int getLayoutRsid() {
        return R.layout.activity_trainnumber;
    }

    @Override
    public void getDataSueecss(String type, CinfoBean.DataBean dataBean) {
        if (mMyDialog != null && mMyDialog.isShowing()) {
            mMyDialog.dismiss();
        }
        this.mDataBean = dataBean;
        mList = dataBean.getList();
        switch (type) {
            case "":  // 进页面加载:
                mTrainnumberAdapter.refreshDataAndNotifyDataSetChanged(dataBean.getList());

                break;
            case "1":
                if (mXrefreshview != null) {
                    mXrefreshview.stopRefresh();
                    cout = 1;
                    if (dataBean.getList().size() == 0) {
                        Toast.makeText(TrainnumberActivity.this,"暂无数据",Toast.LENGTH_SHORT).show();

                    }else {
                        Toast.makeText(TrainnumberActivity.this, "刷新成功", Toast.LENGTH_SHORT).show();
                    }

                    mTrainnumberAdapter.refreshDataAndNotifyDataSetChanged(dataBean.getList());
                }
                break;
            case "2":
                if (mXrefreshview != null) {
                    mXrefreshview.stopLoadMore();
                    Toast.makeText(TrainnumberActivity.this, "加载成功", Toast.LENGTH_SHORT).show();

                    mTrainnumberAdapter.addDataAndNotifyDataSetChanged(dataBean.getList());

                }
                break;
        }



    }

    @Override
    public void getDataFailure() {
        if (mMyDialog != null && mMyDialog.isShowing()) {
            mMyDialog.dismiss();
        }
    }
}
